package com.itheima.web.controller;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.domain.QuestionItem;
import com.itheima.domain.Result;
import com.itheima.factory.BeanFactory;
import com.itheima.service.QuestionItemService;
import com.itheima.utils.BeanUtil;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 该Servlet处理说有的CRUD操作
 * 分页查询: http://localhost:8080/heima_mm/store/questionItem/findByPage?currentPage=1&pageSize=5;
 * 新增:http://localhost:8080/heima_mm/store/questionItem/save
 * 新增:http://localhost:8080/heima_mm/store/questionItem/edit
 * 新增:http://localhost:8080/heima_mm/store/questionItem/delete
 *
 */
@WebServlet("/store/questionItem/*")
public class QuestionItemServlet extends BaseServlet {
       private QuestionItemService questionItemService = BeanFactory.getBean("questionItemService", QuestionItemService.class);

    private Result findAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            //获取问题id
            String questionId = request.getParameter("questionId");

            //调用service,根据id查询信息
            List<QuestionItem> list = questionItemService.findAll(questionId);
            //响应给过到客户端
            return new Result(true,MessageConstant.QUERY_QUESTIONITEM_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.QUERY_QUESTIONITEM_FAIL);
        }


    }
    private Result delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            //1. 获取请求参数并封装
            String id = request.getParameter("id");
            //2.调用service层方法,编辑数据
            questionItemService.delete(id);
            //3.响应结果
            return new Result(true,MessageConstant.DELETE_QUESTIONITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_QUESTIONITEM_FAIL);
        }

    }

    private Result edit(HttpServletRequest request, HttpServletResponse response) throws IOException {
        
        try {
            //1. 获取请求参数并封装
            QuestionItem questionItem = BeanUtil.fillBeanFromJson(request, QuestionItem.class);
            //2.调用service层方法,编辑数据
            
            questionItemService.update(questionItem);
            //3.响应结果
            return new Result(true,MessageConstant.EDIT_QUESTIONITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_QUESTIONITEM_FAIL);
        }


    }

    private Result findById(HttpServletRequest request, HttpServletResponse response) throws IOException {
            
        try {
            //获取请求参数 id
            String id = request.getParameter("id");
            //调用service,根据id查询信息

            QuestionItem questionItem = questionItemService.findById(id);
            //响应给过到客户端
            return new Result(true,MessageConstant.QUERY_QUESTIONITEM_SUCCESS,questionItem);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.QUERY_QUESTIONITEM_FAIL);
        }


    }

    private Result save(HttpServletRequest request, HttpServletResponse response) throws IOException {
        
        try {
            //1. 获取请求参数并封装
            QuestionItem questionItem = BeanUtil.fillBeanFromJson(request, QuestionItem.class);
            //2.调用service层方法,保存数据
            questionItemService.save(questionItem);
            //3.响应结果
            return new Result(true,MessageConstant.SAVE_QUESTIONITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.SAVE_QUESTIONITEM_FAIL);
        }

    }

}
